cmake_minimum_required(VERSION 3.16)
project(unitest)

set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 23)

enable_testing()

find_package(GTest REQUIRED)
include_directories(
        ${GTEST_INCLUDE_DIRS}
        ${CMAKE_CURRENT_SOURCE_DIR}
)

function(add_gtest name files)
    add_executable(${name})
    file(GLOB sources ${files})
    list(FILTER sources INCLUDE REGEX ".*\\.cpp$")
    target_sources(${name} PRIVATE ${sources} main.cpp)

    file(READ ${sources} source_content)
    if(source_content MATCHES "#include.*gmock")
        target_link_libraries(${name} PRIVATE
                GTest::gmock
                GTest::GTest
                GTest::Main
        )
    else()
        target_link_libraries(${name} PRIVATE
                GTest::GTest
                GTest::Main
        )
    endif()
    
    add_test(NAME ${name} COMMAND ${name})
endfunction()

file(GLOB dir_list RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} [0-9][0-9]*)

foreach(target_path ${dir_list})
    if(IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/${target_path})
        get_filename_component(target ${target_path} NAME)
        add_gtest(${target} ${target_path}/*.cpp)
    endif()
endforeach()